package com.ctshk.sapp.custom.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.rpc.custom.dto.quotation.QuotationEditInfoDTO;
import com.ctshk.rpc.custom.dto.quotation.QuotationInfoDTO;
import com.ctshk.rpc.custom.dto.quotation.QuotationListDTO;
import com.ctshk.rpc.custom.req.inquiry.InquiryReq;
import com.ctshk.rpc.custom.req.quotation.QuotationInfoReq;
import com.ctshk.rpc.custom.req.quotation.QuotationListReq;
import com.ctshk.rpc.custom.req.quotation.QuotationReq;
import com.ctshk.rpc.custom.service.ICustomizedQuotationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiKeyAuthDefinition;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;

/**
 * <p>
 * 报价单 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-16
 */
@Api(value="询价单报价", tags="报价单管理")
@RestController
@RequestMapping("/quotation")
public class CustomizedQuotationController extends BaseController {

    @DubboReference
    private ICustomizedQuotationService customizedQuotationService;

    @ApiOperation(value="发起报价", notes="报价-分为询价单报价(带入询价单id)和无询价报价(不带入询价单id)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.ADD, operTitle = "发起报价", operDesc = "发起询价单报价")
    @PostMapping("")
    public Result add(@RequestBody @Valid QuotationReq quotationReq) {
        try {
            return Result.success(customizedQuotationService.add(quotationReq, super.getCurrentUser(getRequest()).getId()));
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="报价单编辑", notes="报价单编辑")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.UPDATE, operTitle = "报价单编辑", operDesc = "报价单编辑")
    @PostMapping("/edit")
    public Result put(@RequestBody @Valid QuotationReq quotationReq) {
        try {
            return Result.success(customizedQuotationService.put(quotationReq, super.getCurrentUser(getRequest()).getId()));
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="报价单列表", notes="报价单列表(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.QUERY, operTitle = "报价单列表", operDesc = "报价单列表")
    @PostMapping("/list")
    public Result<PageResponse<QuotationListDTO>> quotationList(@RequestBody @Valid QuotationListReq quotationListReq) {
        try {
            TokenUser currentUser = getCurrentUser(getRequest());
            PageResponse<QuotationListDTO> list =  customizedQuotationService.list(quotationListReq,currentUser);
            return Result.success(list);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="报价单详情", notes="报价单详情(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.QUERY, operTitle = "报价单详情", operDesc = "报价单详情")
    @PostMapping("/info")
    public Result<QuotationInfoDTO> quotationInfo(@RequestBody @Valid QuotationInfoReq quotationInfoReq) {
        try {
            return customizedQuotationService.query(quotationInfoReq);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="报价单编辑页面详情", notes="报价单编辑页面详情(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.QUERY, operTitle = "报价单详情", operDesc = "报价单详情")
    @PostMapping("/edit-info")
    public Result<QuotationEditInfoDTO> quotationEditInfo(@RequestBody @Valid QuotationInfoReq quotationInfoReq) {
        try {
            return customizedQuotationService.quotationEditInfo(quotationInfoReq);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

}
